package io.tim.mvc.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

/**
 * mvc-config.xml 的解析类 解析后得到一个 Map<String, XmlBean> 其中 String 作为 key 值，记录了action
 * 处理完毕业务之后的数据或显示流转方向，即某个 url XmlBean 记录了包括上面 url 在内的业务处理相关的信息，还包括业务对应的实例化
 * ActionForm，业务处理的具体 Action 等
 * 
 * @author Zhang Nan
 * @since 2017/02/21
 *
 */
public class MVC_Config_Process {

	public MVC_Config_Process() {
		
	}
	
	/**
	 * 解析 mvc-config.xml
	 * @throws Exception
	 */
	public static Map<String, XmlBean> process(String xmlPath) throws Exception {
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(new File(xmlPath));
		Element root = document.getRootElement();
//		Element formbeans = root.getChild("formbeans");
		// 得到 <actionforms> 标签下的所有 <bean>
//		List<Element> forms = formbeans.getChildren();
//		for (Element form : forms) {
//			String formId = form.getAttributeValue("id");
//			String formClass = form.getAttributeValue("class");
//		}
		
		Map<String, XmlBean> xmlBeanMap = new HashMap<String, XmlBean>();
		// 得到 <action-mappings> 标签下的所有 <action>
		Element actionmappings = root.getChild("action-mappings");
		List<Element> actions = actionmappings.getChildren();
		
		for (Element action : actions) {
			XmlBean xmlBean = new XmlBean();
			String actionId = action.getAttributeValue("id");
			xmlBean.setBeanName(action.getAttributeValue("id"));
			
			Element formbeans = root.getChild("formbeans");
			List<Element> forms = formbeans.getChildren();
			// 根据 action 的 id 找到对应的 actionform 的 class值
			for (Element form : forms) {
				String formId = form.getAttributeValue("id");
				if (actionId.equals(formId)) {
//					String formClass = form.getAttributeValue("class");
					xmlBean.setFormClass(form.getAttributeValue("class"));
					break;
				}
			}
			String actionPath = action.getAttributeValue("path");
			xmlBean.setPath(actionPath);
			
			String actionClass = action.getAttributeValue("class");
			xmlBean.setActionClass(actionClass);
			
			List<Element> forwards = action.getChildren();
			Map<String, String> forwardMap = new HashMap<String, String>();
			for (Element forward : forwards) {
//				String forwardId = forward.getAttributeValue("id");
//				String forwardValue = forward.getAttributeValue("value");
				forwardMap.put(forward.getAttributeValue("id"), forward.getAttributeValue("value"));
			}
			xmlBean.setActionForward(forwardMap);
			xmlBeanMap.put(actionPath, xmlBean);
		}
		
		return xmlBeanMap;
	}
	
	public static void main(String[] args) throws Exception {
		Map<String, XmlBean> xMap = new HashMap<String, XmlBean>(); 
		xMap = MVC_Config_Process.process("config/mvc-config.xml");
		/*Set<Entry<String, XmlBean>> eSet = xMap.entrySet();
		for (Iterator<Entry<String, XmlBean>> it = eSet.iterator(); it.hasNext();) {
			@SuppressWarnings("unchecked")
			Entry<String, XmlBean> entry = (Entry<String, XmlBean>) it.next();
			System.out.println("path: " + entry.getKey() + ", XmlBean: " + entry.getValue().toString());
		}*/
		Set<String> keySet = xMap.keySet();
		for (Iterator<String> it = keySet.iterator(); it.hasNext(); ) {
			String key = it.next();
			System.out.println("path: " + key + ", XmlBean: " + xMap.get(key));
		}
	}
}
